//
//  SetVC.m
//  AirQuality
//
//  Created by Po on 15/9/11.
//  Copyright © 2015年 Po. All rights reserved.
//

#import "SetVC.h"
#import "SetCell.h"
#import "MessageVC.h"
#import "SDImageCache.h"

@interface SetVC () <UITableViewDelegate, UITableViewDataSource, SetCellDelegate,UIAlertViewDelegate>

@property (strong, nonatomic) UITableView * tableView;

@property (strong, nonatomic) NSArray * tittleArray;
@property (strong, nonatomic) NSString * versionStr,*trackViewUrl1;

@end

@implementation SetVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _tittleArray = @[@[@"消息中心",@"开启提醒"],@[@"清理缓存"],@[@"退出"]];
    [self.mainView addSubview:[self getTableView]];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

#pragma mark - event
- (void)pressReturnButton
{
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"注销" message:@"确认注销吗？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * comfirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"logOutEvent" object:nil];
    }];
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:comfirm];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 1000 && buttonIndex == 1) {
        [[UIApplication sharedApplication] openURL: [NSURL URLWithString:_trackViewUrl1]];
    }
}

- (void)downLoadWithURl:(NSString *) url{

}

- (void)upAlert{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"当前已是最新版本" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
    [alertView show];
    [self performSelector:@selector(dimissAlert:) withObject:alertView afterDelay:3.0];
}
- (void) dimissAlert:(UIAlertView *)alert {
    if (alert) {
        
       [alert dismissWithClickedButtonIndex:0 animated:YES];
    }
}
/**
 *  清理缓存
 */

- (void)clearDisk
{
    __weak UIViewController * vc = self;
    [RJTools showTitle:@"清理缓存" msg:@"是否清理缓存" cancelString:@"取消" confirmString:@"确定" confirmBlock:^{
        [[SDImageCache sharedImageCache] clearDisk];
        [[SDImageCache sharedImageCache] clearMemory];
        [RJTools showTitle:@"清理缓存" msg:@"清理完成" cancelString:@"知道了" vc:vc];
    } vc:self];
}

#pragma mark - delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * title = _tittleArray[indexPath.section][indexPath.row];
    if ([title isEqualToString:@"消息中心"]) {
        MessageVC * messageVC = [[MessageVC alloc] init];
        [self.navigationController pushViewController:messageVC animated:YES];
    } else if([title isEqualToString:@"清理缓存"]) {
        [self clearDisk];
    } else if([title isEqualToString:@"开启提醒"]){

    }else {
        [self pressReturnButton];
    }
   
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_tittleArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tittleArray[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SetCell * cell = [tableView dequeueReusableCellWithIdentifier:@"SetCell"];
    NSString * tittle = _tittleArray[indexPath.section][indexPath.row];
    if ([tittle isEqualToString:@"退出"]) {
        [cell setBackgroundColor:[UIColor redColor]];
        [cell.tittleLabel setTextAlignment:NSTextAlignmentCenter];
        [cell.tittleLabel setTextColor:[UIColor whiteColor]];
    }
    [cell setDelegate:self];
    [cell.tittleLabel setText:tittle];
    if (indexPath.section == 0 && indexPath.row == 1) {
        [cell.switchButton setHidden:NO];
    } else {
        [cell.switchButton setHidden:YES];
    }
   
    if (indexPath.section == 0) {
        if (indexPath.row  == 2) {

        }
        else{
        [cell.versionLable removeFromSuperview];
        }

    }else{
        [cell.versionLable removeFromSuperview];
    }
    return cell;
}

- (void)SetCell:(SetCell *)cell didSelectSwitchButton:(UISwitch *)switchButton
{
    if ([cell.tittleLabel.text isEqualToString:@"开启提醒"]) {
        [[RJUserData defaultRJUserData] setMessageNotifaction:switchButton.on complect:^(BOOL result) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [RJTools showTitle:@"通知提醒"
                               msg:(switchButton.on ? @"开启了提醒！" : @"关闭了提醒")
                      cancelString:@"知道了"
                                vc:self];
            });
        }];
    }
}

#pragma mark - getter
- (UITableView *)getTableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - sysNavBarHeight - sysTabbarHeight) style:UITableViewStyleGrouped];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        [_tableView registerNib:[UINib nibWithNibName:@"SetCell" bundle:nil] forCellReuseIdentifier:@"SetCell"];
    }
    return _tableView;
}
@end
